package member;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import control.ServerIp;

public class WriteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public WriteServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//request.setCharacterEncoding("euc-kr");
		request.setCharacterEncoding("utf-8");
		ServerIp ip = new ServerIp();
		Connection conn = null;
		Statement stmt = null;
		ResultSet check = null;
		String mb_id = request.getParameter("mb_id");
		try{
			Class.forName(ip.getDriver());
			conn = DriverManager.getConnection(ip.getUrl(),ip.getId(),ip.getPw());
			stmt = conn.createStatement();
			
			String checkSql = "select * from member_info where mb_id='" + mb_id + "'";
			check = stmt.executeQuery(checkSql);
			if(!check.next()){
				Writing writing = new Writing();
				writing.setMb_id(request.getParameter("mb_id"));
				writing.setMb_pw(request.getParameter("mb_pw"));
				writing.setMb_pw_que(request.getParameter("mb_pw_que"));
				writing.setMb_pw_ans(request.getParameter("mb_pw_ans"));
				writing.setMb_name(request.getParameter("mb_name"));
				writing.setMb_zip(request.getParameter("mb_zip"));
				writing.setMb_addr1(request.getParameter("mb_addr1"));
				writing.setMb_addr2(request.getParameter("mb_addr2"));
				writing.setMb_tel(request.getParameter("mb_tel"));
				writing.setMb_phone(request.getParameter("mb_phone"));
				writing.setMb_email(request.getParameter("mb_email"));
				String mb_mailing = request.getParameter("mb_mailing");
				if(mb_mailing == null){
					writing.setMb_mailing("n");
				}else if(mb_mailing.equals("y")){
					writing.setMb_mailing("y");
				}else{
					writing.setMb_mailing("n");
				}
				writing.setMb_login_count(request.getParameter("mb_login_count"));
				writing.setMb_current_date(request.getParameter("mb_current_date"));
				writing.setMb_date(new Timestamp(System.currentTimeMillis()).toString());
				String mb_level = request.getParameter("mb_level");
				if(mb_level == null){
					writing.setMb_level(1);
				}else{
					writing.setMb_level(Integer.valueOf(request.getParameter("mb_level")));
				}
				String mb_point = request.getParameter("mb_point");
				if(mb_point == null){
					writing.setMb_point(0);
				}else{
					writing.setMb_point(Integer.valueOf(request.getParameter("mb_point")));
				}
				WritingManager manager = WritingManager.getInstance();
				manager.insert(writing);
				try{
					stmt.close();
					conn.close();
				}catch(Exception e){}
				RequestDispatcher rd = request.getRequestDispatcher("/Dmyna/member/memberResult.jsp");
				rd.forward(request, response);
			}else{
				try{
					stmt.close();
					conn.close();
				}catch(Exception e){}
				RequestDispatcher rd = request.getRequestDispatcher("/Dmyna/member/memberFalse.jsp");
				rd.forward(request, response);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
